package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.template.MinIOTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.utils.common.threadUtil.ThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Resource
    private MinIOTemplate minIOTemplate;

    @Resource
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //校验参数是否为空
        if (multipartFile == null || multipartFile.isEmpty()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //获取原文件名
        String originalFilename = multipartFile.getOriginalFilename();
        //获取文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //拼接成唯一文件名
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        //上传文件
        String url;
        try {
            url = minIOTemplate.uploadImgFile("", fileName, multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //封装数据保存
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(ThreadLocalUtil.get().getId());
        wmMaterial.setUrl(url);
        wmMaterial.setType(0);
        wmMaterial.setIsCollection(0);
        //插入数据库
        wmMaterialMapper.insert(wmMaterial);

        //返回结果
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findlist(WmMaterialDto dto) {
        //1. 校验参数
        dto.checkParam();

        //2. 封装分页查询条件 , 执行查询
        LambdaQueryWrapper<WmMaterial> wrapper = Wrappers.<WmMaterial>lambdaQuery();
        wrapper.eq(WmMaterial::getUserId, ThreadLocalUtil.get().getId());

        if (dto.getIsCollection() != null && dto.getIsCollection() == 1) {
            wrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        Page<WmMaterial> page = wmMaterialMapper.selectPage(new Page<>(dto.getPage(), dto.getSize()), wrapper);

        //4. 封装数据返回
        ResponseResult result = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }

    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteMaterial(Integer id) {
        try {
            if (id == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            WmMaterial wmMaterial = wmMaterialMapper.selectById(id);
            if (wmMaterial == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            wmMaterialMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(501, "文件删除失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateMaterial(Integer id) {

        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmMaterial material = wmMaterialMapper.selectById(id);
        Integer isCollection = material.getIsCollection();

        material.setIsCollection(isCollection == 0 ? 1 : 0);
        wmMaterialMapper.updateById(material);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
